package thread.demo05_2.ReentrantReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 读写互斥或写读互斥
 * 
 * @author mengzhang6
 *
 */
public class ReentrantReadWriteLockDemo3 {
	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

	public static void main(String[] args) throws InterruptedException {
		ReentrantReadWriteLockDemo3 demo = new ReentrantReadWriteLockDemo3();

		new Thread(() -> demo.read(), "ThreadA").start();
		Thread.sleep(1000);
		new Thread(() -> demo.write(), "ThreadB").start();
	}

	private void read() {
		try {
			try {
				lock.readLock().lock();
				System.out.println("获得读锁" + Thread.currentThread().getName() + " 时间:" + System.currentTimeMillis());
				Thread.sleep(3000);
			} finally {
				lock.readLock().unlock();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void write() {
		try {
			try {
				lock.writeLock().lock();
				System.out.println("获得写锁" + Thread.currentThread().getName() + " 时间:" + System.currentTimeMillis());
				Thread.sleep(3000);
			} finally {
				lock.writeLock().unlock();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
